# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        # if not head: return None
        # self.head_node = ListNode()
        # def rever(head):
        #     if not head.next:
        #         self.head_node = head
        #         return head
        #     node = rever(head.next)
        #     node.next = head
        #     head.next = None
        #     return head
        # rever(head)
        # return self.head_node
        if not head or head.next: return head
        node = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return node

    """Java
    class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode node = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }
}
    """
